#!/usr/bin/env php
<?php

define('SYMFONY_SOURCE_DIR', __DIR__.'/src/Symfony');

require_once '__PHPDIR__/Symfony/Polyfill/autoload.php';
\Fedora\Autoloader\Autoload::addPsr4('Symfony\\', SYMFONY_SOURCE_DIR);

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

$finder = new Finder();
$finder
    ->in(SYMFONY_SOURCE_DIR)
    ->notPath('Tests')
    ->name('composer.json')
    ->sortByName();

foreach ($finder as $composerFile) {
    fprintf(STDERR, 'generating autoloader from %s'.PHP_EOL, $composerFile);
    $autoloadGenerator = new AutoloadGenerator($composerFile);
    echo $autoloadGenerator->getFilename().PHP_EOL;
    echo $autoloadGenerator->getDevFilename().PHP_EOL;
}


//------------------------------------------------------------------------------


final class AutoloadGenerator {
    private static $pkgMap = [
        'cache/integration-tests' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Cache/IntegrationTests/autoload.php',
        ],
        'composer/package-versions-deprecated' => false,
        'doctrine/annotations' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/Common/Annotations/autoload.php',
        ],
        'doctrine/cache' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'Doctrine/Common/Cache2/autoload.php',
                'Doctrine/Common/Cache/autoload.php',
            ],
        ],
        'doctrine/collections' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/Common/Collections/autoload.php',
        ],
        'doctrine/common' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/Common/autoload.php',
        ],
        'doctrine/event-manager' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/Common/EventManager/autoload.php',
        ],
        'doctrine/persistence' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'Doctrine/Persistence2/autoload.php',
                'Doctrine/Common/Persistence/autoload.php',
            ],
        ],
        'doctrine/reflection' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/Common/Reflection/autoload.php',
        ],
        'doctrine/data-fixtures' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/Common/DataFixtures/autoload.php',
        ],
        'doctrine/dbal' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
             'path' => [
                'Doctrine/DBAL3/autoload.php',
                'Doctrine/DBAL/autoload.php',
             ],
        ],
        'doctrine/doctrine-bundle' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'Doctrine/Bundle/DoctrineBundle2/autoload.php',
                'Doctrine/Bundle/DoctrineBundle/autoload.php',
            ],
        ],
        'doctrine/orm' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Doctrine/ORM/autoload.php',
        ],
        'egulias/email-validator' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'Egulias/EmailValidator3/autoload.php',
                'Egulias/EmailValidator2/autoload.php',
            ],
        ],
        'guzzlehttp/promises' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'GuzzleHttp/Promise/autoload.php',
        ],
        'masterminds/html5' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Masterminds/HTML5/autoload.php',
        ],
        'mongodb/mongodb' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'MongoDB/autoload.php',
        ],
        'monolog/monolog' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Monolog/autoload.php',
        ],
        'nyholm/psr7' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Nyholm/Psr7/autoload.php',
        ],
        'friendsofphp/proxy-manager-lts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'FriendsOfPHP/ProxyManager/autoload.php',
        ],
        'phpdocumentor/reflection-docblock' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'phpDocumentor/Reflection/DocBlock5/autoload.php',
                'phpDocumentor/Reflection/DocBlock4/autoload.php',
                'phpDocumentor/Reflection/DocBlock/autoload.php',
            ],
        ],
        'predis/predis' => false,
        'psr/cache-implementation' => false,
        'psr/container-implementation' => false,
        'psr/log-implementation' => false,
        'psr/cache' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Psr/Cache/autoload.php',
        ],
        'psr/container' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Psr/Container/autoload.php',
        ],
        'psr/http-client' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Psr/Http/Client/autoload.php',
        ],
        'psr/link' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Psr/Link/autoload.php',
        ],
        'psr/log' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => 'Psr/Log/autoload.php',
        ],
        'psr/simple-cache' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'Psr/SimpleCache2/autoload.php',
                'Psr/SimpleCache/autoload.php',
            ],
        ],
        'symfony/asset' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Asset/autoload.php',
        ],
        'symfony/browser-kit' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/BrowserKit/autoload.php',
        ],
        'symfony/cache' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Cache/autoload.php',
        ],
        'symfony/class-loader' => false,
        'symfony/config' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Config/autoload.php',
        ],
        'symfony/console' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Console/autoload.php',
        ],
        'symfony/contracts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Contracts/autoload.php',
        ],
        'symfony/cache-contracts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Contracts/autoload.php',
        ],
        'symfony/event-dispatcher-contracts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Contracts/autoload.php',
        ],
        'symfony/http-client-contracts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Contracts/autoload.php',
        ],
        'symfony/service-contracts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Contracts/autoload.php',
        ],
        'symfony/translation-contracts' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Contracts/autoload.php',
        ],
        'symfony/css-selector' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/CssSelector/autoload.php',
        ],
        'symfony/debug-bundle' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bundle/DebugBundle/autoload.php',
        ],
        'symfony/debug' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Debug/autoload.php',
        ],
        'symfony/dependency-injection' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/DependencyInjection/autoload.php',
        ],
        'symfony/doctrine-bridge' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bridge/Doctrine/autoload.php',
        ],
        'symfony/dom-crawler' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/DomCrawler/autoload.php',
        ],
        'symfony/dotenv' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Dotenv/autoload.php',
        ],
        'symfony/error-handler' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/ErrorHandler/autoload.php',
        ],
        'symfony/event-dispatcher' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/EventDispatcher/autoload.php',
        ],
        'symfony/expression-language' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/ExpressionLanguage/autoload.php',
        ],
        'symfony/filesystem' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Filesystem/autoload.php',
        ],
        'symfony/finder' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Finder/autoload.php',
        ],
        'symfony/form' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Form/autoload.php',
        ],
        'symfony/framework-bundle' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bundle/FrameworkBundle/autoload.php',
        ],
        'symfony/http-client' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/HttpClient/autoload.php',
        ],
        'symfony/http-foundation' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/HttpFoundation/autoload.php',
        ],
        'symfony/http-kernel' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/HttpKernel/autoload.php',
        ],
        'symfony/inflector' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Inflector/autoload.php',
        ],
        'symfony/intl' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Intl/autoload.php',
        ],
        'symfony/ldap' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Ldap/autoload.php',
        ],
        'symfony/lock' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Lock/autoload.php',
        ],
        'symfony/mailer' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Mailer/autoload.php',
        ],
        'symfony/amazon-mailer' => false,
        'symfony/google-mailer' => false,
        'symfony/mailgun-mailer' => false,
        'symfony/mailchimp-mailer' => false,
        'symfony/postmark-mailer' => false,
        'symfony/sendgrid-mailer' => false,
        'symfony/messenger' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Messenger/autoload.php',
        ],
        'symfony/mime' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Mime/autoload.php',
        ],
        'symfony/monolog-bridge' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bridge/Monolog/autoload.php',
        ],
        'symfony/options-resolver' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/OptionsResolver/autoload.php',
        ],
        'symfony/phpunit-bridge' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bridge/PhpUnit/autoload.php',
        ],
        'symfony/polyfill-ctype' => false,
        'symfony/polyfill-intl-icu' => false,
        'symfony/polyfill-intl-idn' => false,
        'symfony/polyfill-mbstring' => false,
        'symfony/polyfill-php72' =>
            [
                'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
                'path' => 'Symfony/Polyfill/autoload.php',
            ],
        'symfony/polyfill-php73' =>
            [
                'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
                'path' => 'Symfony/Polyfill/autoload.php',
            ],
        'symfony/polyfill-php74' =>
            [
                'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
                'path' => 'Symfony/Polyfill/autoload.php',
            ],
        'symfony/polyfill-php80' =>
            [
                'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
                'path' => 'Symfony/Polyfill/autoload.php',
            ],
        'symfony/polyfill-php81' =>
            [
                'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
                'path' => 'Symfony/Polyfill/autoload.php',
            ],
        'symfony/process' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Process/autoload.php',
        ],
        'symfony/property-access' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/PropertyAccess/autoload.php',
        ],
        'symfony/property-info' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/PropertyInfo/autoload.php',
        ],
        'symfony/proxy-manager-bridge' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bridge/ProxyManager/autoload.php',
        ],
        'symfony/routing' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Routing/autoload.php',
        ],
        'symfony/security-acl' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR', // external
            'path' => 'Symfony/Component/Security/Acl/autoload.php',
        ],
        'symfony/security-bundle' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bundle/SecurityBundle/autoload.php',
        ],
        'symfony/security-core' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Security/Core/autoload.php',
        ],
        'symfony/security-csrf' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Security/Csrf/autoload.php',
        ],
        'symfony/security-guard' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Security/Guard/autoload.php',
        ],
        'symfony/security-http' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Security/Http/autoload.php',
        ],
        'symfony/security' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Security/autoload.php',
        ],
        'symfony/serializer' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Serializer/autoload.php',
        ],
        'symfony/stopwatch' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Stopwatch/autoload.php',
        ],
        'symfony/templating' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Templating/autoload.php',
        ],
        'symfony/translation' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Translation/autoload.php',
        ],
        'symfony/twig-bridge' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bridge/Twig/autoload.php',
        ],
        'symfony/twig-bundle' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bundle/TwigBundle/autoload.php',
        ],
        'symfony/validator' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Validator/autoload.php',
        ],
        'symfony/var-dumper' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/VarDumper/autoload.php',
        ],
        'symfony/var-exporter' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/VarExporter/autoload.php',
        ],
        'symfony/web-link' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/WebLink/autoload.php',
        ],
        'symfony/web-profiler-bundle' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Bundle/WebProfilerBundle/autoload.php',
        ],
        'symfony/workflow' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Workflow/autoload.php',
        ],
        'symfony/yaml' => [
            'prefix' => 'FEDORA_SYMFONY4_DIR',
            'path' => 'Component/Yaml/autoload.php',
        ],
        'twig/twig' => [
            'prefix' => 'FEDORA_SYMFONY4_PHP_DIR',
            'path' => [
                'Twig3/autoload.php',
                'Twig2/autoload.php',
                'Twig/autoload.php',
            ],
        ],
        'enqueue/messenger-adapter' => false,
        # require-dev only, ignored for now
        'paragonie/sodium_compat' => false,
        'php-http/httplug' => false,
        'twig/cssinliner-extra' => false,
        'twig/inky-extra' => false,
        'twig/markdown-extra' => false,
    ];

    private $filename = null;

    private $devFilename = null;

    public function __construct(SplFileInfo $composerFile) {
        $composerPath = $composerFile->getPath();
        $composerJson = static::composerJson($composerFile);

        // autoload.php
        $content = static::content(
            $composerPath,
            $composerJson['name'],
            isset($composerJson['autoload']['psr-0']) ? $composerJson['autoload']['psr-0'] : [],
            isset($composerJson['autoload']['psr-4']) ? $composerJson['autoload']['psr-4'] : [],
            isset($composerJson['autoload']['files']) ? $composerJson['autoload']['files'] : [],
            isset($composerJson['autoload']['classmap']) ? $composerJson['autoload']['classmap'] : [],
            static::dependencyAutoloaders($composerJson, 'require'),
            static::dependencyAutoloaders($composerJson, 'suggest')
        );
        $this->filename = $composerFile->getPath() . '/autoload.php';
        if (FALSE == file_put_contents($this->filename, $content)) {
            throw new Exception(sprintf(
                'Failed to generate autoload file "%s"',
                $this->filename
            ));
        }

        // autoload-dev.php
        $content = static::content(
            $composerPath,
            $composerJson['name'],
            [],
            [],
            [],
            [],
            static::dependencyAutoloaders($composerJson, 'require-dev'),
            [],
            true
        );
        $this->devFilename = $composerFile->getPath() . '/autoload-dev.php';
        if (FALSE == file_put_contents($this->devFilename, $content)) {
            throw new Exception(sprintf(
                'Failed to generate autoload file "%s"',
                $this->filename
            ));
        }
    }

    private static function composerJson(SplFileInfo $composerFile) {
      $composerJson = json_decode(
          file_get_contents($composerFile->getPathname()),
          true
      );

      if (!isset($composerJson)) {
          throw new \Exception(sprintf(
              'Failed to JSON decode "%s"',
              $composerFile->getPathname()
          ));
      }

      return $composerJson;
    }

    private static function dependencyAutoloaders($composerJson, $composerKey) {
        $dependencyAutoloaders = [];

        if (isset($composerJson[$composerKey])) {
            $dependencies = array_keys(array_filter(
                $composerJson[$composerKey],
                function ($pkg) {
                    return preg_match('#[^/]+/[^/]+#', $pkg);
                },
                ARRAY_FILTER_USE_KEY
            ));

            foreach ($dependencies as $pkg) {
                // Use Symfony cache component as PSR cache implementation
                // for "require-dev" dependency.
                if (
                    ('require-dev' == $composerKey)
                    && ('psr/cache-implementation' == $pkg)
                ) {
                    $pkg = 'symfony/cache';
                }

                if ($autoloader = self::pkg2Autoload($pkg)) {
                    $dependencyAutoloaders[] = $autoloader;
                }
            }

            ksort($dependencyAutoloaders);
        }

        return $dependencyAutoloaders;
    }

    private static function pkg2Autoload ($pkg) {
        if (!isset(self::$pkgMap[$pkg])) {
            throw new Exception(sprintf('No autoload map found for pkg "%s"', $pkg));
        } elseif (empty(self::$pkgMap[$pkg])) {
            return null;
        }

        $map = self::$pkgMap[$pkg];
        $prefix = $map['prefix'];
        $path = $map['path'];

        return is_array($path)
            ? sprintf('[%s]', implode(', ', array_map(function ($map_path) use ($prefix) {
                return sprintf("%s.'/%s'", $prefix, $map_path);
            }, $path)))
            : sprintf("%s.'/%s'", $prefix, $path);
    }

    public function content(
        $path,
        $name,
        array $psr0,
        array $psr4,
        array $files,
        array $classmap,
        array $dependencyAutoloadersRequired,
        array $dependencyAutoloadersOptional,
        $dev = false
    ) {
        $pkg = explode('/', $name)[1];

        $content = <<<AUTOLOAD
<?php
/**
 * Autoloader for php-symfony4-${pkg} and its' dependencies
 * (created by php-symfony4-__VERSION__-__RELEASE__).
 */

AUTOLOAD;

        // This switch statement handles the "autoload-common" require for sub-sub-modules.
        if (!$dev) {
            switch ($pkg) {
                case 'security-core':
                case 'security-csrf':
                case 'security-guard':
                case 'security-http':
                    $content .= "require_once dirname(dirname(dirname(__DIR__))).'/autoload-common.php';".PHP_EOL;
                    break;
                default:
                    $content .= "require_once dirname(dirname(__DIR__)).'/autoload-common.php';".PHP_EOL;
            }
        } else {
            switch ($pkg) {
                case 'security-core':
                case 'security-csrf':
                case 'security-guard':
                case 'security-http':
                    $content .= "require_once dirname(__DIR__).'/autoload.php';".PHP_EOL;
                    break;
                default:
                    $content .= "require_once __DIR__.'/autoload.php';".PHP_EOL;
            }
        }

        // PSR-0
        if (!empty($psr0)) {
            $content .= PHP_EOL.'// Self PSR-0'.PHP_EOL;

            foreach ($psr0 as $namespace => $directory) {
                $content .= sprintf(
                    "\\Fedora\\Autoloader\\Autoload::addPsr0('%s', %s, true);".PHP_EOL,
                    str_replace('\\', '\\\\', $namespace),
                    $directory ? "'$directory'" : '__DIR__'
                );
            }
        }

        // PSR-4
        if (!empty($psr4)) {
            $content .= PHP_EOL.'// Self PSR-4'.PHP_EOL;

            foreach ($psr4 as $namespace => $directory) {
              $content .= sprintf(
                  "\\Fedora\\Autoloader\\Autoload::addPsr4('%s', %s, true);".PHP_EOL,
                  str_replace('\\', '\\\\', $namespace),
                  $directory ? "'$directory'" : '__DIR__'
              );
            }
        }

        // Files
        if (!empty($files)) {
            $content .= PHP_EOL.'// Self files'.PHP_EOL;

            foreach ($files as $file) {
                $content .= sprintf(
                  "require_once __DIR__.'/$file';".PHP_EOL,
                  $file
                );
            }
        }

        // Classmap
        if (!empty($classmap)) {
            $cmd = array_merge(
              [
                  'phpab',
                  '--template', 'fedora',
                  '--output', 'autoload.classmap.php',
              ],
              $classmap
            );

            $process = new Process($cmd, $path);
            $process->run();

            if (!$process->isSuccessful()) {
                throw new ProcessFailedException($process);
            }

            $content .= <<<SELF_CLASSMAP

// Self classmap
require_once __DIR__.'/autoload.classmap.php';

SELF_CLASSMAP;
        }

        // Required dependencies
        if (!empty($dependencyAutoloadersRequired)) {
            $dependencyAutoloadersRequiredString = implode(",\n    ", $dependencyAutoloadersRequired);
            $content .= <<<DEPENDENCY_AUTOLOADERS_REQUIRED

// Required dependencies
\Fedora\Autoloader\Dependencies::required([
    $dependencyAutoloadersRequiredString
]);

DEPENDENCY_AUTOLOADERS_REQUIRED;
        }

        // Optional dependencies
        if (!empty($dependencyAutoloadersOptional)) {
            $dependencyAutoloadersOptionalString = implode(",\n    ", $dependencyAutoloadersOptional);
            $content .= <<<DEPENDENCY_AUTOLOADERS_REQUIRED

// Optional dependencies
\Fedora\Autoloader\Dependencies::optional([
    $dependencyAutoloadersOptionalString
]);
DEPENDENCY_AUTOLOADERS_REQUIRED;
        }

        return $content.PHP_EOL;
    }

    public function getFilename() {
        return $this->filename;
    }

    public function getDevFilename() {
        return $this->devFilename;
    }
}
